<template>
  <div class="container">
    <h1>图书购买清单</h1>

    <!-- 添加图书表单 -->
    <div class="add-book-form">
      <div class="form-group">
        <label for="bookName">图书名称：</label>
        <select v-model="selectedBookIndex" @change="onSelectBook" class="book-select">
          <option value="-1">请选择默认图书</option>
          <option v-for="(book, idx) in defaultBooks" :key="book.name" :value="idx">
            {{ book.name }}（￥{{ book.price }}）
          </option>
        </select>
        <input
            type="text"
            id="bookName"
            v-model="newBook.name"
            placeholder="请输入图书名称"
        >
      </div>

      <div class="form-group">
        <label for="bookPrice">单价 (￥)：</label>
        <input
            type="number"
            id="bookPrice"
            v-model.number="newBook.price"
            placeholder="请输入单价"
            min="0"
        >
      </div>

      <div class="form-group">
        <label for="bookCount">数量：</label>
        <input
            type="number"
            id="bookCount"
            v-model.number="newBook.count"
            placeholder="请输入数量"
            min="1"
        >
      </div>

      <button
          @click="addBook"
          :disabled="!isValidBook"
          class="add-btn"
      >
        添加图书
      </button>
    </div>

    <!-- 使用子组件显示图书列表 -->
    <BookTable
        :books="books"
        @delete="deleteBook"
    >
      <!-- 使用插槽定义表头 -->
      <template #header>
        <tr>
          <th>#</th>
          <th>图书名称</th>
          <th>单价 (￥)</th>
          <th>数量</th>
          <th>小计 (￥)</th>
          <th>操作</th>
        </tr>
      </template>


    </BookTable>

    <!-- 显示总价 -->
    <div class="total-price" v-if="books.length > 0">
      总价: {{ totalPrice.toFixed(2) }} 元
    </div>

    <div class="empty-message" v-else>
      购物清单为空，请添加图书！
    </div>
  </div>
</template>

<script>
import BookTable from './components/BookTable.vue'

export default {
  name: 'App',
  components: {
    BookTable
  },
  data() {
    return {
      // 新书信息对象
      newBook: {
        name: '',
        price: '',
        count: 1
      },
      // 图书列表数组
      books: [
        { id: 1, name: 'Vue.js 入门', price: 39.9, count: 2 },
        { id: 2, name: 'JavaScript 高级程序设计', price: 79.5, count: 1 },
        { id: 3, name: '前端工程化实战', price: 56.0, count: 1 }
      ],
      // 默认图书列表
      defaultBooks: [
        { name: 'Vue.js 入门', price: 39.9 },
        { name: 'JavaScript 高级程序设计', price: 79.5 },
        { name: '前端工程化实战', price: 56.0 },
        { name: '算法图解', price: 68.0 },
        { name: '深入浅出Node.js', price: 59.0 }
      ],
      // 下拉菜单选中的索引
      selectedBookIndex: -1,
      // 用于生成唯一ID
      nextId: 1
    }
  },
  computed: {
    // 计算有效性
    isValidBook() {
      return this.newBook.name &&
          this.newBook.price > 0 &&
          this.newBook.count > 0
    },
    // 计算总价
    totalPrice() {
      return this.books.reduce((sum, book) => {
        return sum + (book.price * book.count)
      }, 0)
    }
  },
  methods: {
    // 选择默认图书后自动填充
    onSelectBook() {
      if (this.selectedBookIndex !== -1) {
        const book = this.defaultBooks[this.selectedBookIndex]
        this.newBook.name = book.name
        this.newBook.price = book.price
      } else {
        this.newBook.name = ''
        this.newBook.price = ''
      }
    },
    // 添加新图书到列表
    addBook() {
      if (!this.isValidBook) return

      this.books.push({
        id: this.nextId++,
        name: this.newBook.name,
        price: this.newBook.price,
        count: this.newBook.count
      })

      // 重置表单
      this.newBook = {
        name: '',
        price: '',
        count: 1
      }
    },
    // 删除指定ID的图书
    deleteBook(id) {
      const index = this.books.findIndex(book => book.id === id)
      if (index !== -1) {
        this.books.splice(index, 1)
      }
    }
  }
}
</script>

<style scoped>
.container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  font-family: Arial, sans-serif;
}

h1 {
  text-align: center;
  margin-bottom: 30px;
}

.add-book-form {
  box-shadow: 0 4px 16px rgba(60, 120, 200, 0.10);
  border: 1.5px solid #e0e7ef;

  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  margin-bottom: 30px;
  padding: 20px;
  border-radius: 8px;
  background-color: #f5f5f5;
}

.form-group {
  margin-bottom: 8px;

  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 150px;
}

label {
  margin-bottom: 5px;
  font-weight: bold;
}

input, .book-select {
  padding: 8px;
  border: 1.5px solid #b8c1d1;
  border-radius: 6px;
  background: #fafdff;
  font-size: 1rem;
  transition: border 0.2s, box-shadow 0.2s;
  margin-bottom: 4px;
}

input:focus, .book-select:focus {
  border-color: #4CAF50;
  outline: none;
  box-shadow: 0 0 0 2px #d4f5df;
}

.book-select {
  width: 100%;
  margin-bottom: 6px;
}

.add-btn {
  transition: background 0.2s, box-shadow 0.2s;
  box-shadow: 0 2px 8px rgba(76,175,80,0.10);
  font-size: 1rem;
  letter-spacing: 1px;

  align-self: flex-end;
  padding: 8px 16px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.add-btn:disabled {
  opacity: 0.6;
  box-shadow: none;

  background-color: #cccccc;
  cursor: not-allowed;
}

.total-price {
  margin-top: 20px;
  text-align: right;
  font-size: 1.2em;
  font-weight: bold;
}

.empty-message {
  margin-top: 20px;
  text-align: center;
  color: #666;
}

.delete-btn {
  background-color: #f44336;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 5px 10px;
  cursor: pointer;
}
</style>